#include <iostream>
#include <stdio.h>
#include"SeqList.h"
#include"Match.h"
#include "fstream"

using namespace std;


//顺序表的初始化
void InitSeqList(SeqList& L)
{
    ifstream infile;
    infile.open("meta.txt", ios::in);
    char buf[1024] = { 0 };
    int cnt = 0;
    while (infile >> buf) {
        if (cnt % 3 == 0) {
            for (char s : buf) {
                if (s == 0) break;
                L.elem[cnt / 3].name.push_back(s);
            }
        }
        else if (cnt % 3 == 1) {
            L.elem[cnt / 3].difficulty = buf[0] - '0';
        }
        else if (cnt % 3 == 2) {
            L.elem[cnt / 3].hours = buf[0] - '0';
            L.length++;
        }
        cnt++;
    }
}

SeqList* SLmatch(SeqList& L, string name) {
    int len_title = ListLength(L);
    int ans_tag;
    int pos = 0;
    wstring in, title;
    int len_str = name.length();

    KLmeta ans_ifnull;
    string ans_null = "抱歉，未找到搜索结果";
    ans_ifnull.name = ans_null;
    ans_ifnull.difficulty = 99999;
    ans_ifnull.hours = 99999;


    in = string2wstring(name);

    vector<vector<int>> dis(0, vector<int>(2));
    SeqList* ans =new SeqList(20);
    for (int i = 0; i < len_title; i++) {
        title = string2wstring(L.elem[i].name);
        ans_tag = KMP_Sunday(in, title);
        if (ans_tag != -1) {
            SeqListPush_back(*ans, L.elem[i]);
        }
    }

    if (SLEmpty(*ans)) {
        for (int i = 0; i < len_title; i++) {
            title = string2wstring(L.elem[i].name);
            ans_tag = minDis(in, title);
            dis.push_back(vector<int>{i, ans_tag});
        }

        QuickSort(dis, 0, len_title - 1);


        for (int i = 0; i < 5; i++) {
            pos = dis[i][0];
            //只有差异数小于输入字符串的长度的一半时才存入结果
            if(dis[i][1]<=len_str/4)
            SeqListPush_back(*ans, L.elem[pos]);
        }

    }
    //模糊匹配没有合适的结果，返回匹配失败
    if (SLEmpty(*ans)) {
        SeqListPush_back(*ans,ans_ifnull);
    }

    return ans;

}

int SLsearch(SeqList& L,string name){
    for(int i = 0;i < L.length;i  ++){
        if(L.elem[i].name==name) return i;
    }
    return -1;
}

//判断线性表是否为空
bool SLEmpty(SeqList& L) {
    return (L.length == 0);
}

void SeqListPush_back(SeqList& L, KLmeta k) {
    L.elem[L.length] = k;
    L.length++;
}

//在顺序表L的i位置，插入元素e
void SeqListInsert(SeqList& L, int i, KLmeta e)
{//在顺序表L的中的第i个位置，插入新元素e，i值的合法范围是1≤i≤L.length+10

    if ((i < 1) || (i > L.length + 10)) //判定i位置是否合法
    {
        //cout << "i位置不合法" << endl;
        return;
    }

    if (L.length == L.size) //当前顺序表存储空间已满
    {
        //cout << "当前顺序表存储空间已满" << endl;
        return;
    }

    for (int j = L.length - 1; j >= i - 1; j--) //插入的位置及后面的元素都向后移一位
    {
        L.elem[j + 1] = L.elem[j];
    }

    L.elem[i - 1] = e; //将新元素放在第i位置
    L.length++; //顺序表长度+1
}

//删除位置i处的元素
void SeqListDelete(SeqList& L, int i) {
    //位置合法性
    if (i<1 || i>L.length) {
        //cout << "i位置不合法" << endl;
        return;
    }
    //i后的元素向前移动一位
    for (int j = i; j < L.length; j++)
    {
        L.elem[j - 1] = L.elem[j];
    }
    L.length--;
}

//清空线性表中的数据
void ClearList(SeqList& L) {
    L.length = 0;
    cout << "\n已清空线性表\n" << endl;
}

//销毁线性表
void DestroySeqList(SeqList& L) {
    if (!L.elem) {
        cout << "线性表不存在" << endl;
    }
    free(L.elem);
    L.elem = NULL;
    cout << "\n线性表已销毁\n" << endl;;
}

void OutPutSL(SeqList& L){
    ofstream ofile;
    ofile.open("meta.txt", ios::out);
    for(int i = 0;i < L.length;i ++){
        string str = L.elem[i].name + " " + to_string(L.elem[i].difficulty) + " " + to_string(L.elem[i].hours)+"\n";
        ofile << str;
    }
}

//线性表的长度
int ListLength(SeqList& L) {
    return L.length;
}
